<template>
  <div>
    <xw-pagenav :visible.sync="visible" />
    <!-- 表格展示 -->
    <el-card class="box-card">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="编号" width="60"> </el-table-column>
        <el-table-column prop="showname" label="角色中文名称" width="180"> </el-table-column>
        <el-table-column prop="name" label="角色英文名称" width="180"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="prop">
            <el-button size="mini" type="primary" style="margin-right: 10px">修改</el-button>
            <el-button size="mini" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <br />
      <el-pagination layout="prev, pager, next" :page-size="pageSize" :total="total" @current-change="handleChange">
      </el-pagination>
    </el-card>

    <!-- 添加弹出层 -->
    <xw-dialog :title="title" :visible.sync="visible" :addHandler="addHandler">
      <el-form slot="add" :model="formData" status-icon :rules="createRules()" ref="ruleForm" label-width="100px">
        <el-form-item label="角色名称" prop="showname">
          <el-input v-model="formData.showname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="英文名称" prop="name">
          <el-input v-model="formData.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
    </xw-dialog>
  </div>
</template>

<script>
import { addRoleApi, roleListApi } from '@/api/roleApi'
export default {
  data() {
    return {
      title: '添加角色',
      visible: false,
      pageSize: 10,
      total: 100,
      // 表格数据
      tableData: [],
      formData: {
        showname: '',
        name: ''
      }
    }
  },
  mounted() {
    this.handleChange()
  },
  methods: {
    // 获取数据  分页
    async handleChange(page = 1) {
      const ret = await roleListApi(page)
      this.tableData = ret.data
    },
    addHandler() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          // 网络请求
          const ret = await addRoleApi(this.formData)
          if (ret.code == 0) {
            this.$message({
              type: 'success',
              message: '添加角色成功',
              duration: 1500,
              onClose: () => {
                this.formData.name = ''
                this.visible = false
              }
            })
          } else {
            this.$message({
              duration: 1500,
              message: '添加角色失败',
              type: 'error'
            })
          }
        } else {
          this.$message({
            duration: 1500,
            message: '验证不通过',
            type: 'error'
          })
        }
      })
    },
    createRules() {
      return {
        showname: [{ required: true, message: '名称必填写' }],
        name: [
          { required: true, message: '角色名称必填写' },
          {
            validator: (rule, value, callback) => {
              if (/^\s+$/.test(value)) {
                return callback(new Error('角色名称不能为空格'))
              }
              if (/\s/.test(value)) {
                return callback(new Error('角色名称不合法'))
              }
              return callback()
            }
          }
        ]
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  width: 100%;
}
</style>
